"""
数据库连接标准写法
"""
from pymysql import Connection

conn = None
cursor = None
try:
    # 1、连接数据库对象
    conn = Connection(
        user="root",
        password="123456",
        host="localhost",
        database="mydb1",
        port=3306,
        charset="utf8"
    )
    # 2、创建游标对象
    cursor = conn.cursor()
    # 3、使用游标对象执行sql
    sql = '''
    CREATE TABLE hero(
        id INT PRIMARY KEY AUTO_INCREMENT,
        h_name VARCHAR(30),
        age INT,
        gender VARCHAR(5),
        address VARCHAR(100)
    )
    '''
    cursor.execute(sql)
    # 4、将数据提交到数据库或者获取数据
    conn.commit()
except Exception as e:
    print(e)
#5、关闭游标对象
finally:
    if conn:
        conn.close()
#6、关闭数据库连接对象
    if cursor:
        cursor.close()